home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / sun4.md / gdb / values.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-24  |  40.8 KB  |  1,478 lines

  1. /* Low level packing and unpacking of values for GDB, the GNU Debugger.
  2.    Copyright 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include <string.h>
  22. #include "symtab.h"
  23. #include "gdbtypes.h"
  24. #include "value.h"
  25. #include "gdbcore.h"
  26. #include "frame.h"
  27. #include "command.h"
  28. #include "gdbcmd.h"
  29. #include "target.h"
  30.  
  31. /* Local function prototypes. */
  32.  
  33. static value
  34. value_headof PARAMS ((value, struct type *, struct type *));
  35.  
  36. static void
  37. show_values PARAMS ((char *, int));
  38.  
  39. static void
  40. show_convenience PARAMS ((char *, int));
  41.  
  42. /* The value-history records all the values printed
  43.    by print commands during this session.  Each chunk
  44.    records 60 consecutive values.  The first chunk on
  45.    the chain records the most recent values.
  46.    The total number of values is in value_history_count.  */
  47.  
  48. #define VALUE_HISTORY_CHUNK 60
  49.  
  50. struct value_history_chunk
  51. {
  52.   struct value_history_chunk *next;
  53.   value values[VALUE_HISTORY_CHUNK];
  54. };
  55.  
  56. /* Chain of chunks now in use.  */
  57.  
  58. static struct value_history_chunk *value_history_chain;
  59.  
  60. static int value_history_count;    /* Abs number of last entry stored */
  61.  
  62. /* List of all value objects currently allocated
  63.    (except for those released by calls to release_value)
  64.    This is so they can be freed after each command.  */
  65.  
  66. static value all_values;
  67.  
  68. /* Allocate a  value  that has the correct length for type TYPE.  */
  69.  
  70. value
  71. allocate_value (type)
  72.      struct type *type;
  73. {
  74.   register value val;
  75.  
  76.   check_stub_type (type);
  77.  
  78.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
  79.   VALUE_NEXT (val) = all_values;
  80.   all_values = val;
  81.   VALUE_TYPE (val) = type;
  82.   VALUE_LVAL (val) = not_lval;
  83.   VALUE_ADDRESS (val) = 0;
  84.   VALUE_FRAME (val) = 0;
  85.   VALUE_OFFSET (val) = 0;
  86.   VALUE_BITPOS (val) = 0;
  87.   VALUE_BITSIZE (val) = 0;
  88.   VALUE_REPEATED (val) = 0;
  89.   VALUE_REPETITIONS (val) = 0;
  90.   VALUE_REGNO (val) = -1;
  91.   VALUE_LAZY (val) = 0;
  92.   VALUE_OPTIMIZED_OUT (val) = 0;
  93.   return val;
  94. }
  95.  
  96. /* Allocate a  value  that has the correct length
  97.    for COUNT repetitions type TYPE.  */
  98.  
  99. value
  100. allocate_repeat_value (type, count)
  101.      struct type *type;
  102.      int count;
  103. {
  104.   register value val;
  105.  
  106.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
  107.   VALUE_NEXT (val) = all_values;
  108.   all_values = val;
  109.   VALUE_TYPE (val) = type;
  110.   VALUE_LVAL (val) = not_lval;
  111.   VALUE_ADDRESS (val) = 0;
  112.   VALUE_FRAME (val) = 0;
  113.   VALUE_OFFSET (val) = 0;
  114.   VALUE_BITPOS (val) = 0;
  115.   VALUE_BITSIZE (val) = 0;
  116.   VALUE_REPEATED (val) = 1;
  117.   VALUE_REPETITIONS (val) = count;
  118.   VALUE_REGNO (val) = -1;
  119.   VALUE_LAZY (val) = 0;
  120.   VALUE_OPTIMIZED_OUT (val) = 0;
  121.   return val;
  122. }
  123.  
  124. /* Return a mark in the value chain.  All values allocated after the
  125.    mark is obtained (except for those released) are subject to being freed
  126.    if a subsequent value_free_to_mark is passed the mark.  */
  127. value
  128. value_mark ()
  129. {
  130.   return all_values;
  131. }
  132.  
  133. /* Free all values allocated since MARK was obtained by value_mark
  134.    (except for those released).  */
  135. void
  136. value_free_to_mark (mark)
  137.      value mark;
  138. {
  139.   value val, next;
  140.  
  141.   for (val = all_values; val && val != mark; val = next)
  142.     {
  143.       next = VALUE_NEXT (val);
  144.       value_free (val);
  145.     }
  146.   all_values = val;
  147. }
  148.  
  149. /* Free all the values that have been allocated (except for those released).
  150.    Called after each command, successful or not.  */
  151.  
  152. void
  153. free_all_values ()
  154. {
  155.   register value val, next;
  156.  
  157.   for (val = all_values; val; val = next)
  158.     {
  159.       next = VALUE_NEXT (val);
  160.       value_free (val);
  161.     }
  162.  
  163.   all_values = 0;
  164. }
  165.  
  166. /* Remove VAL from the chain all_values
  167.    so it will not be freed automatically.  */
  168.  
  169. void
  170. release_value (val)
  171.      register value val;
  172. {
  173.   register value v;
  174.  
  175.   if (all_values == val)
  176.     {
  177.       all_values = val->next;
  178.       return;
  179.     }
  180.  
  181.   for (v = all_values; v; v = v->next)
  182.     {
  183.       if (v->next == val)
  184.     {
  185.       v->next = val->next;
  186.       break;
  187.     }
  188.     }
  189. }
  190.  
  191. /* Return a copy of the value ARG.
  192.    It contains the same contents, for same memory address,
  193.    but it's a different block of storage.  */
  194.  
  195. value
  196. value_copy (arg)
  197.      value arg;
  198. {
  199.   register value val;
  200.   register struct type *type = VALUE_TYPE (arg);
  201.   if (VALUE_REPEATED (arg))
  202.     val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
  203.   else
  204.     val = allocate_value (type);
  205.   VALUE_LVAL (val) = VALUE_LVAL (arg);
  206.   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
  207.   VALUE_OFFSET (val) = VALUE_OFFSET (arg);
  208.   VALUE_BITPOS (val) = VALUE_BITPOS (arg);
  209.   VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
  210.   VALUE_REGNO (val) = VALUE_REGNO (arg);
  211.   VALUE_LAZY (val) = VALUE_LAZY (arg);
  212.   if (!VALUE_LAZY (val))
  213.     {
  214.       bcopy (VALUE_CONTENTS_RAW (arg), VALUE_CONTENTS_RAW (val),
  215.          TYPE_LENGTH (VALUE_TYPE (arg))
  216.          * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
  217.     }
  218.   return val;
  219. }
  220.  
  221. /* Access to the value history.  */
  222.  
  223. /* Record a new value in the value history.
  224.    Returns the absolute history index of the entry.
  225.    Result of -1 indicates the value was not saved; otherwise it is the
  226.    value history index of this new item.  */
  227.  
  228. int
  229. record_latest_value (val)
  230.      value val;
  231. {
  232.   int i;
  233.  
  234.   /* Check error now if about to store an invalid float.  We return -1
  235.      to the caller, but allow them to continue, e.g. to print it as "Nan". */
  236.   if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) {
  237.     (void) unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
  238.     if (i) return -1;        /* Indicate value not saved in history */
  239.   }
  240.  
  241.   /* Here we treat value_history_count as origin-zero
  242.      and applying to the value being stored now.  */
  243.  
  244.   i = value_history_count % VALUE_HISTORY_CHUNK;
  245.   if (i == 0)
  246.     {
  247.       register struct value_history_chunk *new
  248.     = (struct value_history_chunk *)
  249.       xmalloc (sizeof (struct value_history_chunk));
  250.       bzero (new->values, sizeof new->values);
  251.       new->next = value_history_chain;
  252.       value_history_chain = new;
  253.     }
  254.  
  255.   value_history_chain->values[i] = val;
  256.   release_value (val);
  257.  
  258.   /* Now we regard value_history_count as origin-one
  259.      and applying to the value just stored.  */
  260.  
  261.   return ++value_history_count;
  262. }
  263.  
  264. /* Return a copy of the value in the history with sequence number NUM.  */
  265.  
  266. value
  267. access_value_history (num)
  268.      int num;
  269. {
  270.   register struct value_history_chunk *chunk;
  271.   register int i;
  272.   register int absnum = num;
  273.  
  274.   if (absnum <= 0)
  275.     absnum += value_history_count;
  276.  
  277.   if (absnum <= 0)
  278.     {
  279.       if (num == 0)
  280.     error ("The history is empty.");
  281.       else if (num == 1)
  282.     error ("There is only one value in the history.");
  283.       else
  284.     error ("History does not go back to $$%d.", -num);
  285.     }
  286.   if (absnum > value_history_count)
  287.     error ("History has not yet reached $%d.", absnum);
  288.  
  289.   absnum--;
  290.  
  291.   /* Now absnum is always absolute and origin zero.  */
  292.  
  293.   chunk = value_history_chain;
  294.   for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
  295.        i > 0; i--)
  296.     chunk = chunk->next;
  297.  
  298.   return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
  299. }
  300.  
  301. /* Clear the value history entirely.
  302.    Must be done when new symbol tables are loaded,
  303.    because the type pointers become invalid.  */
  304.  
  305. void
  306. clear_value_history ()
  307. {
  308.   register struct value_history_chunk *next;
  309.   register int i;
  310.   register value val;
  311.  
  312.   while (value_history_chain)
  313.     {
  314.       for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
  315.     if (val = value_history_chain->values[i])
  316.       free ((PTR)val);
  317.       next = value_history_chain->next;
  318.       free ((PTR)value_history_chain);
  319.       value_history_chain = next;
  320.     }
  321.   value_history_count = 0;
  322. }
  323.  
  324. static void
  325. show_values (num_exp, from_tty)
  326.      char *num_exp;
  327.      int from_tty;
  328. {
  329.   register int i;
  330.   register value val;
  331.   static int num = 1;
  332.  
  333.   if (num_exp)
  334.     {
  335.       if (num_exp[0] == '+' && num_exp[1] == '\0')
  336.     /* "info history +" should print from the stored position.  */
  337.     ;
  338.       else
  339.     /* "info history <exp>" should print around value number <exp>.  */
  340.     num = parse_and_eval_address (num_exp) - 5;
  341.     }
  342.   else
  343.     {
  344.       /* "info history" means print the last 10 values.  */
  345.       num = value_history_count - 9;
  346.     }
  347.  
  348.   if (num <= 0)
  349.     num = 1;
  350.  
  351.   for (i = num; i < num + 10 && i <= value_history_count; i++)
  352.     {
  353.       val = access_value_history (i);
  354.       printf_filtered ("$%d = ", i);
  355.       value_print (val, stdout, 0, Val_pretty_default);
  356.       printf_filtered ("\n");
  357.     }
  358.  
  359.   /* The next "info history +" should start after what we just printed.  */
  360.   num += 10;
  361.  
  362.   /* Hitting just return after this command should do the same thing as
  363.      "info history +".  If num_exp is null, this is unnecessary, since
  364.      "info history +" is not useful after "info history".  */
  365.   if (from_tty && num_exp)
  366.     {
  367.       num_exp[0] = '+';
  368.       num_exp[1] = '\0';
  369.     }
  370. }
  371.  
  372. /* Internal variables.  These are variables within the debugger
  373.    that hold values assigned by debugger commands.
  374.    The user refers to them with a '$' prefix
  375.    that does not appear in the variable names stored internally.  */
  376.  
  377. static struct internalvar *internalvars;
  378.  
  379. /* Look up an internal variable with name NAME.  NAME should not
  380.    normally include a dollar sign.
  381.  
  382.    If the specified internal variable does not exist,
  383.    one is created, with a void value.  */
  384.  
  385. struct internalvar *
  386. lookup_internalvar (name)
  387.      char *name;
  388. {
  389.   register struct internalvar *var;
  390.  
  391.   for (var = internalvars; var; var = var->next)
  392.     if (!strcmp (var->name, name))
  393.       return var;
  394.  
  395.   var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
  396.   var->name = concat (name, NULL);
  397.   var->value = allocate_value (builtin_type_void);
  398.   release_value (var->value);
  399.   var->next = internalvars;
  400.   internalvars = var;
  401.   return var;
  402. }
  403.  
  404. value
  405. value_of_internalvar (var)
  406.      struct internalvar *var;
  407. {
  408.   register value val;
  409.  
  410. #ifdef IS_TRAPPED_INTERNALVAR
  411.   if (IS_TRAPPED_INTERNALVAR (var->name))
  412.     return VALUE_OF_TRAPPED_INTERNALVAR (var);
  413. #endif 
  414.  
  415.   val = value_copy (var->value);
  416.   if (VALUE_LAZY (val))
  417.     value_fetch_lazy (val);
  418.   VALUE_LVAL (val) = lval_internalvar;
  419.   VALUE_INTERNALVAR (val) = var;
  420.   return val;
  421. }
  422.  
  423. void
  424. set_internalvar_component (var, offset, bitpos, bitsize, newval)
  425.      struct internalvar *var;
  426.      int offset, bitpos, bitsize;
  427.      value newval;
  428. {
  429.   register char *addr = VALUE_CONTENTS (var->value) + offset;
  430.  
  431. #ifdef IS_TRAPPED_INTERNALVAR
  432.   if (IS_TRAPPED_INTERNALVAR (var->name))
  433.     SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
  434. #endif
  435.  
  436.   if (bitsize)
  437.     modify_field (addr, (int) value_as_long (newval),
  438.           bitpos, bitsize);
  439.   else
  440.     bcopy (VALUE_CONTENTS (newval), addr,
  441.        TYPE_LENGTH (VALUE_TYPE (newval)));
  442. }
  443.  
  444. void
  445. set_internalvar (var, val)
  446.      struct internalvar *var;
  447.      value val;
  448. {
  449. #ifdef IS_TRAPPED_INTERNALVAR
  450.   if (IS_TRAPPED_INTERNALVAR (var->name))
  451.     SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
  452. #endif
  453.  
  454.   free ((PTR)var->value);
  455.   var->value = value_copy (val);
  456.   release_value (var->value);
  457. }
  458.  
  459. char *
  460. internalvar_name (var)
  461.      struct internalvar *var;
  462. {
  463.   return var->name;
  464. }
  465.  
  466. /* Free all internalvars.  Done when new symtabs are loaded,
  467.    because that makes the values invalid.  */
  468.  
  469. void
  470. clear_internalvars ()
  471. {
  472.   register struct internalvar *var;
  473.  
  474.   while (internalvars)
  475.     {
  476.       var = internalvars;
  477.       internalvars = var->next;
  478.       free ((PTR)var->name);
  479.       free ((PTR)var->value);
  480.       free ((PTR)var);
  481.     }
  482. }
  483.  
  484. static void
  485. show_convenience (ignore, from_tty)
  486.      char *ignore;
  487.      int from_tty;
  488. {
  489.   register struct internalvar *var;
  490.   int varseen = 0;
  491.  
  492.   for (var = internalvars; var; var = var->next)
  493.     {
  494. #ifdef IS_TRAPPED_INTERNALVAR
  495.       if (IS_TRAPPED_INTERNALVAR (var->name))
  496.     continue;
  497. #endif
  498.       if (!varseen)
  499.     {
  500.       varseen = 1;
  501.     }
  502.       printf_filtered ("$%s = ", var->name);
  503.       value_print (var->value, stdout, 0, Val_pretty_default);
  504.       printf_filtered ("\n");
  505.     }
  506.   if (!varseen)
  507.     printf ("No debugger convenience variables now defined.\n\
  508. Convenience variables have names starting with \"$\";\n\
  509. use \"set\" as in \"set $foo = 5\" to define them.\n");
  510. }
  511.  
  512. /* Extract a value as a C number (either long or double).
  513.    Knows how to convert fixed values to double, or
  514.    floating values to long.
  515.    Does not deallocate the value.  */
  516.  
  517. LONGEST
  518. value_as_long (val)
  519.      register value val;
  520. {
  521.   /* This coerces arrays and functions, which is necessary (e.g.
  522.      in disassemble_command).  It also dereferences references, which
  523.      I suspect is the most logical thing to do.  */
  524.   if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_ENUM)
  525.     COERCE_ARRAY (val);
  526.   return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
  527. }
  528.  
  529. double
  530. value_as_double (val)
  531.      register value val;
  532. {
  533.   double foo;
  534.   int inv;
  535.   
  536.   foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
  537.   if (inv)
  538.     error ("Invalid floating value found in program.");
  539.   return foo;
  540. }
  541. /* Extract a value as a C pointer.
  542.    Does not deallocate the value.  */
  543. CORE_ADDR
  544. value_as_pointer (val)
  545.      value val;
  546. {
  547.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  548.      whether we want this to be true eventually.  */
  549.   return value_as_long (val);
  550. }
  551.  
  552. /* Unpack raw data (copied from debugee, target byte order) at VALADDR
  553.    as a long, or as a double, assuming the raw data is described
  554.    by type TYPE.  Knows how to convert different sizes of values
  555.    and can convert between fixed and floating point.  We don't assume
  556.    any alignment for the raw data.  Return value is in host byte order.
  557.  
  558.    If you want functions and arrays to be coerced to pointers, and
  559.    references to be dereferenced, call value_as_long() instead.
  560.  
  561.    C++: It is assumed that the front-end has taken care of
  562.    all matters concerning pointers to members.  A pointer
  563.    to member which reaches here is considered to be equivalent
  564.    to an INT (or some size).  After all, it is only an offset.  */
  565.  
  566. /* FIXME:  This should be rewritten as a switch statement for speed and
  567.    ease of comprehension.  */
  568.  
  569. LONGEST
  570. unpack_long (type, valaddr)
  571.      struct type *type;
  572.      char *valaddr;
  573. {
  574.   register enum type_code code = TYPE_CODE (type);
  575.   register int len = TYPE_LENGTH (type);
  576.   register int nosign = TYPE_UNSIGNED (type);
  577.  
  578. #ifdef KGDB
  579.   union { 
  580.    double    foo; /* double forces alignment. */
  581.    char    buffer[8];
  582.   } tmpMem;
  583.   bcopy(valaddr,tmpMem.buffer,8);
  584.   valaddr = tmpMem.buffer;
  585. #endif
  586.  
  587.   if (code == TYPE_CODE_ENUM || code == TYPE_CODE_BOOL)
  588.     code = TYPE_CODE_INT;
  589.   if (code == TYPE_CODE_FLT)
  590.     {
  591.       if (len == sizeof (float))
  592.     {
  593.       float retval;
  594.       bcopy (valaddr, &retval, sizeof (retval));
  595.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  596.       return retval;
  597.     }
  598.  
  599.       if (len == sizeof (double))
  600.     {
  601.       double retval;
  602.       bcopy (valaddr, &retval, sizeof (retval));
  603.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  604.       return retval;
  605.     }
  606.       else
  607.     {
  608.       error ("Unexpected type of floating point number.");
  609.     }
  610.     }
  611.   else if (code == TYPE_CODE_INT && nosign)
  612.     {
  613.       if (len == sizeof (char))
  614.     {
  615.       unsigned char retval = * (unsigned char *) valaddr;
  616.       /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
  617.       return retval;
  618.     }
  619.  
  620.       if (len == sizeof (short))
  621.     {
  622.       unsigned short retval;
  623.       bcopy (valaddr, &retval, sizeof (retval));
  624.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  625.       return retval;
  626.     }
  627.  
  628.       if (len == sizeof (int))
  629.     {
  630.       unsigned int retval;
  631.       bcopy (valaddr, &retval, sizeof (retval));
  632.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  633.       return retval;
  634.     }
  635.  
  636.       if (len == sizeof (long))
  637.     {
  638.       unsigned long retval;
  639.       bcopy (valaddr, &retval, sizeof (retval));
  640.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  641.       return retval;
  642.     }
  643. #ifdef LONG_LONG
  644.       if (len == sizeof (long long))
  645.     {
  646.       unsigned long long retval;
  647.       bcopy (valaddr, &retval, sizeof (retval));
  648.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  649.       return retval;
  650.     }
  651. #endif
  652.       else
  653.     {
  654.       error ("That operation is not possible on an integer of that size.");
  655.     }
  656.     }
  657.   else if (code == TYPE_CODE_INT)
  658.     {
  659.       if (len == sizeof (char))
  660.     {
  661.       SIGNED char retval;    /* plain chars might be unsigned on host */
  662.       bcopy (valaddr, &retval, sizeof (retval));
  663.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  664.       return retval;
  665.     }
  666.  
  667.       if (len == sizeof (short))
  668.     {
  669.       short retval;
  670.       bcopy (valaddr, &retval, sizeof (retval));
  671.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  672.       return retval;
  673.     }
  674.  
  675.       if (len == sizeof (int))
  676.     {
  677.       int retval;
  678.       bcopy (valaddr, &retval, sizeof (retval));
  679.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  680.       return retval;
  681.     }
  682.  
  683.       if (len == sizeof (long))
  684.     {
  685.       long retval;
  686.       bcopy (valaddr, &retval, sizeof (retval));
  687.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  688.       return retval;
  689.     }
  690.  
  691. #ifdef LONG_LONG
  692.       if (len == sizeof (long long))
  693.     {
  694.       long long retval;
  695.       bcopy (valaddr, &retval, sizeof (retval));
  696.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  697.       return retval;
  698.     }
  699. #endif
  700.       else
  701.     {
  702.       error ("That operation is not possible on an integer of that size.");
  703.     }
  704.     }
  705.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  706.      whether we want this to be true eventually.  */
  707.   else if (code == TYPE_CODE_PTR
  708.        || code == TYPE_CODE_REF)
  709.     {
  710.       if (len == sizeof(long))
  711.       {
  712.     long retval;
  713.     bcopy (valaddr, &retval, sizeof(retval));
  714.     SWAP_TARGET_AND_HOST (&retval, sizeof(retval));
  715.     return retval;
  716.       }
  717.       else if (len == sizeof(short))
  718.       {
  719.     short retval;
  720.     bcopy (valaddr, &retval, len);
  721.     SWAP_TARGET_AND_HOST (&retval, len);
  722.     return retval;
  723.       }
  724.     }
  725.   else if (code == TYPE_CODE_MEMBER)
  726.     error ("not implemented: member types in unpack_long");
  727.   else if (code == TYPE_CODE_CHAR)
  728.     return *(unsigned char *)valaddr;
  729.  
  730.   error ("Value not integer or pointer.");
  731.   return 0;     /* For lint -- never reached */
  732. }
  733.  
  734. /* Return a double value from the specified type and address.
  735.    INVP points to an int which is set to 0 for valid value,
  736.    1 for invalid value (bad float format).  In either case,
  737.    the returned double is OK to use.  Argument is in target
  738.    format, result is in host format.  */
  739.  
  740. double
  741. unpack_double (type, valaddr, invp)
  742.      struct type *type;
  743.      char *valaddr;
  744.      int *invp;
  745. {
  746.   register enum type_code code = TYPE_CODE (type);
  747.   register int len = TYPE_LENGTH (type);
  748.   register int nosign = TYPE_UNSIGNED (type);
  749.  
  750. #ifdef KGDB
  751.   union { 
  752.    double    foo; /* double forces alignment. */
  753.    char    buffer[8];
  754.   } tmpMem;
  755.   bcopy(valaddr,tmpMem.buffer,8);
  756.   valaddr = tmpMem.buffer;
  757. #endif
  758.  
  759.   *invp = 0;            /* Assume valid.   */
  760.   if (code == TYPE_CODE_FLT)
  761.     {
  762.       if (INVALID_FLOAT (valaddr, len))
  763.     {
  764.       *invp = 1;
  765.       return 1.234567891011121314;
  766.     }
  767.  
  768.       if (len == sizeof (float))
  769.     {
  770.       float retval;
  771.       bcopy (valaddr, &retval, sizeof (retval));
  772.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  773.       return retval;
  774.     }
  775.  
  776.       if (len == sizeof (double))
  777.     {
  778.       double retval;
  779.       bcopy (valaddr, &retval, sizeof (retval));
  780.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  781.       return retval;
  782.     }
  783.       else
  784.     {
  785.       error ("Unexpected type of floating point number.");
  786.       return 0; /* Placate lint.  */
  787.     }
  788.     }
  789.   else if (nosign) {
  790.    /* Unsigned -- be sure we compensate for signed LONGEST.  */
  791. #ifdef LONG_LONG
  792.    return (unsigned long long) unpack_long (type, valaddr);
  793. #else
  794.    return (unsigned long     ) unpack_long (type, valaddr);
  795. #endif
  796.   } else {
  797.     /* Signed -- we are OK with unpack_long.  */
  798.     return unpack_long (type, valaddr);
  799.   }
  800. }
  801.  
  802. /* Unpack raw data (copied from debugee, target byte order) at VALADDR
  803.    as a CORE_ADDR, assuming the raw data is described by type TYPE.
  804.    We don't assume any alignment for the raw data.  Return value is in
  805.    host byte order.
  806.  
  807.    If you want functions and arrays to be coerced to pointers, and
  808.    references to be dereferenced, call value_as_pointer() instead.
  809.  
  810.    C++: It is assumed that the front-end has taken care of
  811.    all matters concerning pointers to members.  A pointer
  812.    to member which reaches here is considered to be equivalent
  813.    to an INT (or some size).  After all, it is only an offset.  */
  814.  
  815. CORE_ADDR
  816. unpack_pointer (type, valaddr)
  817.      struct type *type;
  818.      char *valaddr;
  819. {
  820. #if 0
  821.   /* The user should be able to use an int (e.g. 0x7892) in contexts
  822.      where a pointer is expected.  So this doesn't do enough.  */
  823.   register enum type_code code = TYPE_CODE (type);
  824.   register int len = TYPE_LENGTH (type);
  825.  
  826.   if (code == TYPE_CODE_PTR
  827.       || code == TYPE_CODE_REF)
  828.     {
  829.       if (len == sizeof (CORE_ADDR))
  830.     {
  831.       CORE_ADDR retval;
  832.       bcopy (valaddr, &retval, sizeof (retval));
  833.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  834.       return retval;
  835.     }
  836.       error ("Unrecognized pointer size.");
  837.     }
  838.   else if (code == TYPE_CODE_MEMBER)
  839.     error ("not implemented: member types in unpack_pointer");
  840.  
  841.   error ("Value is not a pointer.");
  842.   return 0;     /* For lint -- never reached */
  843. #else
  844.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  845.      whether we want this to be true eventually.  */
  846.   return unpack_long (type, valaddr);
  847. #endif
  848. }
  849.  
  850. /* Given a value ARG1 (offset by OFFSET bytes)
  851.    of a struct or union type ARG_TYPE,
  852.    extract and return the value of one of its fields.
  853.    FIELDNO says which field.
  854.  
  855.    For C++, must also be able to return values from static fields */
  856.  
  857. value
  858. value_primitive_field (arg1, offset, fieldno, arg_type)
  859.      register value arg1;
  860.      int offset;
  861.      register int fieldno;
  862.      register struct type *arg_type;
  863. {
  864.   register value v;
  865.   register struct type *type;
  866.  
  867.   check_stub_type (arg_type);
  868.   type = TYPE_FIELD_TYPE (arg_type, fieldno);
  869.  
  870.   /* Handle packed fields */
  871.  
  872.   offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
  873.   if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
  874.     {
  875.       v = value_from_longest (type,
  876.                unpack_field_as_long (arg_type,
  877.                          VALUE_CONTENTS (arg1),
  878.                          fieldno));
  879.       VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
  880.       VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno);
  881.     }
  882.   else
  883.     {
  884.       v = allocate_value (type);
  885.       if (VALUE_LAZY (arg1))
  886.     VALUE_LAZY (v) = 1;
  887.       else
  888.     bcopy (VALUE_CONTENTS_RAW (arg1) + offset,
  889.            VALUE_CONTENTS_RAW (v),
  890.            TYPE_LENGTH (type));
  891.     }
  892.   VALUE_LVAL (v) = VALUE_LVAL (arg1);
  893.   if (VALUE_LVAL (arg1) == lval_internalvar)
  894.     VALUE_LVAL (v) = lval_internalvar_component;
  895.   VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
  896.   VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
  897.   return v;
  898. }
  899.  
  900. /* Given a value ARG1 of a struct or union type,
  901.    extract and return the value of one of its fields.
  902.    FIELDNO says which field.
  903.  
  904.    For C++, must also be able to return values from static fields */
  905.  
  906. value
  907. value_field (arg1, fieldno)
  908.      register value arg1;
  909.      register int fieldno;
  910. {
  911.   return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1));
  912. }
  913.  
  914. /* Return a non-virtual function as a value.
  915.    F is the list of member functions which contains the desired method.
  916.    J is an index into F which provides the desired method. */
  917.  
  918. value
  919. value_fn_field (f, j)
  920.      struct fn_field *f;
  921.      int j;
  922. {
  923.   register value v;
  924.   register struct type *type = TYPE_FN_FIELD_TYPE (f, j);
  925.   struct symbol *sym;
  926.  
  927.   sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
  928.                0, VAR_NAMESPACE, 0, NULL);
  929.   if (! sym) error ("Internal error: could not find physical method named %s",
  930.             TYPE_FN_FIELD_PHYSNAME (f, j));
  931.   
  932.   v = allocate_value (type);
  933.   VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  934.   VALUE_TYPE (v) = type;
  935.   return v;
  936. }
  937.  
  938. /* Return a virtual function as a value.
  939.    ARG1 is the object which provides the virtual function
  940.    table pointer.  ARG1 is side-effected in calling this function.
  941.    F is the list of member functions which contains the desired virtual
  942.    function.
  943.    J is an index into F which provides the desired virtual function.
  944.  
  945.    TYPE is the type in which F is located.  */
  946. value
  947. value_virtual_fn_field (arg1, f, j, type)
  948.      value arg1;
  949.      struct fn_field *f;
  950.      int j;
  951.      struct type *type;
  952. {
  953.   /* First, get the virtual function table pointer.  That comes
  954.      with a strange type, so cast it to type `pointer to long' (which
  955.      should serve just fine as a function type).  Then, index into
  956.      the table, and convert final value to appropriate function type.  */
  957.   value entry, vfn, vtbl;
  958.   value vi = value_from_longest (builtin_type_int, 
  959.                   (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
  960.   struct type *fcontext = TYPE_FN_FIELD_FCONTEXT (f, j);
  961.   struct type *context;
  962.   if (fcontext == NULL)
  963.    /* We don't have an fcontext (e.g. the program was compiled with
  964.       g++ version 1).  Try to get the vtbl from the TYPE_VPTR_BASETYPE.
  965.       This won't work right for multiple inheritance, but at least we
  966.       should do as well as GDB 3.x did.  */
  967.     fcontext = TYPE_VPTR_BASETYPE (type);
  968.   context = lookup_pointer_type (fcontext);
  969.   /* Now context is a pointer to the basetype containing the vtbl.  */
  970.   if (TYPE_TARGET_TYPE (context) != VALUE_TYPE (arg1))
  971.     arg1 = value_ind (value_cast (context, value_addr (arg1)));
  972.  
  973.   context = VALUE_TYPE (arg1);
  974.   /* Now context is the basetype containing the vtbl.  */
  975.  
  976.   /* This type may have been defined before its virtual function table
  977.      was.  If so, fill in the virtual function table entry for the
  978.      type now.  */
  979.   if (TYPE_VPTR_FIELDNO (context) < 0)
  980.     fill_in_vptr_fieldno (context);
  981.  
  982.   /* The virtual function table is now an array of structures
  983.      which have the form { int16 offset, delta; void *pfn; }.  */
  984.   vtbl = value_ind (value_field (arg1, TYPE_VPTR_FIELDNO (context)));
  985.  
  986.   /* Index into the virtual function table.  This is hard-coded because
  987.      looking up a field is not cheap, and it may be important to save
  988.      time, e.g. if the user has set a conditional breakpoint calling
  989.      a virtual function.  */
  990.   entry = value_subscript (vtbl, vi);
  991.  
  992.   /* Move the `this' pointer according to the virtual function table.  */
  993.   VALUE_OFFSET (arg1) += value_as_long (value_field (entry, 0));
  994.   if (! VALUE_LAZY (arg1))
  995.     {
  996.       VALUE_LAZY (arg1) = 1;
  997.       value_fetch_lazy (arg1);
  998.     }
  999.  
  1000.   vfn = value_field (entry, 2);
  1001.   /* Reinstantiate the function pointer with the correct type.  */
  1002.   VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
  1003.  
  1004.   return vfn;
  1005. }
  1006.  
  1007. /* ARG is a pointer to an object we know to be at least
  1008.    a DTYPE.  BTYPE is the most derived basetype that has
  1009.    already been searched (and need not be searched again).
  1010.    After looking at the vtables between BTYPE and DTYPE,
  1011.    return the most derived type we find.  The caller must
  1012.    be satisfied when the return value == DTYPE.
  1013.  
  1014.    FIXME-tiemann: should work with dossier entries as well.  */
  1015.  
  1016. static value
  1017. value_headof (arg, btype, dtype)
  1018.      value arg;
  1019.      struct type *btype, *dtype;
  1020. {
  1021.   /* First collect the vtables we must look at for this object.  */
  1022.   /* FIXME-tiemann: right now, just look at top-most vtable.  */
  1023.   value vtbl, entry, best_entry = 0;
  1024.   /* FIXME: entry_type is never used.  */
  1025.   struct type *entry_type;
  1026.   int i, nelems;
  1027.   int offset, best_offset = 0;
  1028.   struct symbol *sym;
  1029.   CORE_ADDR pc_for_sym;
  1030.   char *demangled_name;
  1031.   struct minimal_symbol *msymbol;
  1032.  
  1033.   btype = TYPE_VPTR_BASETYPE (dtype);
  1034.   check_stub_type (btype);
  1035.   if (btype != dtype)
  1036.     vtbl = value_cast (lookup_pointer_type (btype), arg);
  1037.   else
  1038.     vtbl = arg;
  1039.   vtbl = value_ind (value_field (value_ind (vtbl), TYPE_VPTR_FIELDNO (btype)));
  1040.  
  1041.   /* Check that VTBL looks like it points to a virtual function table.  */
  1042.   msymbol = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtbl));
  1043.   if (msymbol == NULL
  1044.       || !VTBL_PREFIX_P (demangled_name = msymbol -> name))
  1045.     {
  1046.       /* If we expected to find a vtable, but did not, let the user
  1047.      know that we aren't happy, but don't throw an error.
  1048.      FIXME: there has to be a better way to do this.  */
  1049.       struct type *error_type = (struct type *)xmalloc (sizeof (struct type));
  1050.       bcopy (VALUE_TYPE (arg), error_type, sizeof (struct type));
  1051.       TYPE_NAME (error_type) = savestring ("suspicious *", sizeof ("suspicious *"));
  1052.       VALUE_TYPE (arg) = error_type;
  1053.       return arg;
  1054.     }
  1055.  
  1056.   /* Now search through the virtual function table.  */
  1057.   entry = value_ind (vtbl);
  1058.   nelems = longest_to_int (value_as_long (value_field (entry, 2)));
  1059.   for (i = 1; i <= nelems; i++)
  1060.     {
  1061.       entry = value_subscript (vtbl, value_from_longest (builtin_type_int, 
  1062.                               (LONGEST) i));
  1063.       offset = longest_to_int (value_as_long (value_field (entry, 0)));
  1064.       /* If we use '<=' we can handle single inheritance
  1065.        * where all offsets are zero - just use the first entry found. */
  1066.       if (offset <= best_offset)
  1067.     {
  1068.       best_offset = offset;
  1069.       best_entry = entry;
  1070.     }
  1071.     }
  1072.   /* Move the pointer according to BEST_ENTRY's offset, and figure
  1073.      out what type we should return as the new pointer.  */
  1074.   if (best_entry == 0)
  1075.     {
  1076.       /* An alternative method (which should no longer be necessary).
  1077.        * But we leave it in for future use, when we will hopefully
  1078.        * have optimizes the vtable to use thunks instead of offsets. */
  1079.       /* Use the name of vtable itself to extract a base type. */
  1080.       demangled_name += 4;  /* Skip _vt$ prefix. */
  1081.     }
  1082.   else
  1083.     {
  1084.       pc_for_sym = value_as_pointer (value_field (best_entry, 2));
  1085.       sym = find_pc_function (pc_for_sym);
  1086.       demangled_name = cplus_demangle (SYMBOL_NAME (sym), -1);
  1087.       *(strchr (demangled_name, ':')) = '\0';
  1088.     }
  1089.   sym = lookup_symbol (demangled_name, 0, VAR_NAMESPACE, 0, 0);
  1090.   if (sym == 0)
  1091.     error ("could not find type declaration for `%s'", SYMBOL_NAME (sym));
  1092.   if (best_entry)
  1093.     {
  1094.       free (demangled_name);
  1095.       arg = value_add (value_cast (builtin_type_int, arg),
  1096.                value_field (best_entry, 0));
  1097.     }
  1098.   VALUE_TYPE (arg) = lookup_pointer_type (SYMBOL_TYPE (sym));
  1099.   return arg;
  1100. }
  1101.  
  1102. /* ARG is a pointer object of type TYPE.  If TYPE has virtual
  1103.    function tables, probe ARG's tables (including the vtables
  1104.    of its baseclasses) to figure out the most derived type that ARG
  1105.    could actually be a pointer to.  */
  1106.  
  1107. value
  1108. value_from_vtable_info (arg, type)
  1109.      value arg;
  1110.      struct type *type;
  1111. {
  1112.   /* Take care of preliminaries.  */
  1113.   if (TYPE_VPTR_FIELDNO (type) < 0)
  1114.     fill_in_vptr_fieldno (type);
  1115.   if (TYPE_VPTR_FIELDNO (type) < 0 || VALUE_REPEATED (arg))
  1116.     return 0;
  1117.  
  1118.   return value_headof (arg, 0, type);
  1119. }
  1120.  
  1121. /* Compute the address of the baseclass which is
  1122.    the INDEXth baseclass of class TYPE.  The TYPE base
  1123.    of the object is at VALADDR.
  1124.  
  1125.    If ERRP is non-NULL, set *ERRP to be the errno code of any error,
  1126.    or 0 if no error.  In that case the return value is not the address
  1127.    of the baseclasss, but the address which could not be read
  1128.    successfully.  */
  1129.  
  1130. char *
  1131. baseclass_addr (type, index, valaddr, valuep, errp)
  1132.      struct type *type;
  1133.      int index;
  1134.      char *valaddr;
  1135.      value *valuep;
  1136.      int *errp;
  1137. {
  1138.   struct type *basetype = TYPE_BASECLASS (type, index);
  1139.  
  1140.   if (errp)
  1141.     *errp = 0;
  1142.  
  1143.   if (BASETYPE_VIA_VIRTUAL (type, index))
  1144.     {
  1145.       /* Must hunt for the pointer to this virtual baseclass.  */
  1146.       register int i, len = TYPE_NFIELDS (type);
  1147.       register int n_baseclasses = TYPE_N_BASECLASSES (type);
  1148.       char *vbase_name, *type_name = type_name_no_tag (basetype);
  1149.  
  1150.       vbase_name = (char *)alloca (strlen (type_name) + 8);
  1151.       sprintf (vbase_name, "_vb$%s", type_name);
  1152.       /* First look for the virtual baseclass pointer
  1153.      in the fields.  */
  1154.       for (i = n_baseclasses; i < len; i++)
  1155.     {
  1156.       if (! strcmp (vbase_name, TYPE_FIELD_NAME (type, i)))
  1157.         {
  1158.           value val = allocate_value (basetype);
  1159.           CORE_ADDR addr;
  1160.           int status;
  1161.  
  1162.           addr
  1163.         = unpack_pointer (TYPE_FIELD_TYPE (type, i),
  1164.                   valaddr + (TYPE_FIELD_BITPOS (type, i) / 8));
  1165.  
  1166.           status = target_read_memory (addr,
  1167.                        VALUE_CONTENTS_RAW (val),
  1168.                        TYPE_LENGTH (basetype));
  1169.           VALUE_LVAL (val) = lval_memory;
  1170.           VALUE_ADDRESS (val) = addr;
  1171.  
  1172.           if (status != 0)
  1173.         {
  1174.           if (valuep)
  1175.             *valuep = NULL;
  1176.           release_value (val);
  1177.           value_free (val);
  1178.           if (errp)
  1179.             *errp = status;
  1180.           return (char *)addr;
  1181.         }
  1182.           else
  1183.         {
  1184.           if (valuep)
  1185.             *valuep = val;
  1186.           return (char *) VALUE_CONTENTS (val);
  1187.         }
  1188.         }
  1189.     }
  1190.       /* Not in the fields, so try looking through the baseclasses.  */
  1191.       for (i = index+1; i < n_baseclasses; i++)
  1192.     {
  1193.       char *baddr;
  1194.  
  1195.       baddr = baseclass_addr (type, i, valaddr, valuep, errp);
  1196.       if (baddr)
  1197.         return baddr;
  1198.     }
  1199.       /* Not found.  */
  1200.       if (valuep)
  1201.     *valuep = 0;
  1202.       return 0;
  1203.     }
  1204.  
  1205.   /* Baseclass is easily computed.  */
  1206.   if (valuep)
  1207.     *valuep = 0;
  1208.   return valaddr + TYPE_BASECLASS_BITPOS (type, index) / 8;
  1209. }
  1210.  
  1211. long
  1212. unpack_field_as_long (type, valaddr, fieldno)
  1213.      struct type *type;
  1214.      char *valaddr;
  1215.      int fieldno;
  1216. {
  1217.   unsigned long val;
  1218.   int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
  1219.   int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
  1220.  
  1221.   bcopy (valaddr + bitpos / 8, &val, sizeof val);
  1222.   SWAP_TARGET_AND_HOST (&val, sizeof val);
  1223.  
  1224.   /* Extracting bits depends on endianness of the machine.  */
  1225. #if BITS_BIG_ENDIAN
  1226.   val = val >> (sizeof val * 8 - bitpos % 8 - bitsize);
  1227. #else
  1228.   val = val >> (bitpos % 8);
  1229. #endif
  1230.  
  1231.   if (bitsize < 8 * sizeof (val))
  1232.     val &= (((unsigned long)1) << bitsize) - 1;
  1233.   return val;
  1234. }
  1235.  
  1236. /* Modify the value of a bitfield.  ADDR points to a block of memory in
  1237.    target byte order; the bitfield starts in the byte pointed to.  FIELDVAL
  1238.    is the desired value of the field, in host byte order.  BITPOS and BITSIZE
  1239.    indicate which bits (in target bit order) comprise the bitfield.  */
  1240.  
  1241. void
  1242. modify_field (addr, fieldval, bitpos, bitsize)
  1243.      char *addr;
  1244.      int fieldval;
  1245.      int bitpos, bitsize;
  1246. {
  1247.   long oword;
  1248.  
  1249.   /* Reject values too big to fit in the field in question,
  1250.      otherwise adjoining fields may be corrupted.  */
  1251.   if (bitsize < (8 * sizeof (fieldval))
  1252.       && 0 != (fieldval & ~((1<<bitsize)-1)))
  1253.     error ("Value %d does not fit in %d bits.", fieldval, bitsize);
  1254.   
  1255.   bcopy (addr, &oword, sizeof oword);
  1256.   SWAP_TARGET_AND_HOST (&oword, sizeof oword);        /* To host format */
  1257.  
  1258.   /* Shifting for bit field depends on endianness of the target machine.  */
  1259. #if BITS_BIG_ENDIAN
  1260.   bitpos = sizeof (oword) * 8 - bitpos - bitsize;
  1261. #endif
  1262.  
  1263.   /* Mask out old value, while avoiding shifts >= longword size */
  1264.   if (bitsize < 8 * sizeof (oword))
  1265.     oword &= ~(((((unsigned long)1) << bitsize) - 1) << bitpos);
  1266.   else
  1267.     oword &= ~((-1) << bitpos);
  1268.   oword |= fieldval << bitpos;
  1269.  
  1270.   SWAP_TARGET_AND_HOST (&oword, sizeof oword);        /* To target format */
  1271.   bcopy (&oword, addr, sizeof oword);
  1272. }
  1273.  
  1274. /* Convert C numbers into newly allocated values */
  1275.  
  1276. value
  1277. value_from_longest (type, num)
  1278.      struct type *type;
  1279.      register LONGEST num;
  1280. {
  1281.   register value val = allocate_value (type);
  1282.   register enum type_code code = TYPE_CODE (type);
  1283.   register int len = TYPE_LENGTH (type);
  1284.  
  1285.   /* FIXME, we assume that pointers have the same form and byte order as
  1286.      integers, and that all pointers have the same form.  */
  1287.   if (code == TYPE_CODE_INT  || code == TYPE_CODE_ENUM || 
  1288.       code == TYPE_CODE_CHAR || code == TYPE_CODE_PTR ||
  1289.       code == TYPE_CODE_REF)
  1290.     {
  1291.       if (len == sizeof (char))
  1292.     * (char *) VALUE_CONTENTS_RAW (val) = num;
  1293.       else if (len == sizeof (short))
  1294.     * (short *) VALUE_CONTENTS_RAW (val) = num;
  1295.       else if (len == sizeof (int))
  1296.     * (int *) VALUE_CONTENTS_RAW (val) = num;
  1297.       else if (len == sizeof (long))
  1298.     * (long *) VALUE_CONTENTS_RAW (val) = num;
  1299. #ifdef LONG_LONG
  1300.       else if (len == sizeof (long long))
  1301.     * (long long *) VALUE_CONTENTS_RAW (val) = num;
  1302. #endif
  1303.       else
  1304.     error ("Integer type encountered with unexpected data length.");
  1305.     }
  1306.   else
  1307.     error ("Unexpected type encountered for integer constant.");
  1308.  
  1309.   /* num was in host byte order.  So now put the value's contents
  1310.      into target byte order.  */
  1311.   SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
  1312.  
  1313.   return val;
  1314. }
  1315.  
  1316. value
  1317. value_from_double (type, num)
  1318.      struct type *type;
  1319.      double num;
  1320. {
  1321.   register value val = allocate_value (type);
  1322.   register enum type_code code = TYPE_CODE (type);
  1323.   register int len = TYPE_LENGTH (type);
  1324.  
  1325.   if (code == TYPE_CODE_FLT)
  1326.     {
  1327.       if (len == sizeof (float))
  1328.     * (float *) VALUE_CONTENTS_RAW (val) = num;
  1329.       else if (len == sizeof (double))
  1330.     * (double *) VALUE_CONTENTS_RAW (val) = num;
  1331.       else
  1332.     error ("Floating type encountered with unexpected data length.");
  1333.     }
  1334.   else
  1335.     error ("Unexpected type encountered for floating constant.");
  1336.  
  1337.   /* num was in host byte order.  So now put the value's contents
  1338.      into target byte order.  */
  1339.   SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
  1340.  
  1341.   return val;
  1342. }
  1343.  
  1344. /* Deal with the value that is "about to be returned".  */
  1345.  
  1346. /* Return the value that a function returning now
  1347.    would be returning to its caller, assuming its type is VALTYPE.
  1348.    RETBUF is where we look for what ought to be the contents
  1349.    of the registers (in raw form).  This is because it is often
  1350.    desirable to restore old values to those registers
  1351.    after saving the contents of interest, and then call
  1352.    this function using the saved values.
  1353.    struct_return is non-zero when the function in question is
  1354.    using the structure return conventions on the machine in question;
  1355.    0 when it is using the value returning conventions (this often
  1356.    means returning pointer to where structure is vs. returning value). */
  1357.  
  1358. value
  1359. value_being_returned (valtype, retbuf, struct_return)
  1360.      register struct type *valtype;
  1361.      char retbuf[REGISTER_BYTES];
  1362.      int struct_return;
  1363.      /*ARGSUSED*/
  1364. {
  1365.   register value val;
  1366.   CORE_ADDR addr;
  1367.  
  1368. #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
  1369.   /* If this is not defined, just use EXTRACT_RETURN_VALUE instead.  */
  1370.   if (struct_return) {
  1371.     addr = EXTRACT_STRUCT_VALUE_ADDRESS (retbuf);
  1372.     if (!addr)
  1373.       error ("Function return value unknown");
  1374.     return value_at (valtype, addr);
  1375.   }
  1376. #endif
  1377.  
  1378.   val = allocate_value (valtype);
  1379.   EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS_RAW (val));
  1380.  
  1381.   return val;
  1382. }
  1383.  
  1384. /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
  1385.    EXTRACT_RETURN_VALUE?  GCC_P is true if compiled with gcc
  1386.    and TYPE is the type (which is known to be struct, union or array).
  1387.  
  1388.    On most machines, the struct convention is used unless we are
  1389.    using gcc and the type is of a special size.  */
  1390. #if !defined (USE_STRUCT_CONVENTION)
  1391. #define USE_STRUCT_CONVENTION(gcc_p, type)\
  1392.   (!((gcc_p) && (TYPE_LENGTH (value_type) == 1                \
  1393.          || TYPE_LENGTH (value_type) == 2             \
  1394.              || TYPE_LENGTH (value_type) == 4             \
  1395.          || TYPE_LENGTH (value_type) == 8             \
  1396.          )                                            \
  1397.      ))
  1398. #endif
  1399.  
  1400. /* Return true if the function specified is using the structure returning
  1401.    convention on this machine to return arguments, or 0 if it is using
  1402.    the value returning convention.  FUNCTION is the value representing
  1403.    the function, FUNCADDR is the address of the function, and VALUE_TYPE
  1404.    is the type returned by the function.  GCC_P is nonzero if compiled
  1405.    with GCC.  */
  1406.  
  1407. int
  1408. using_struct_return (function, funcaddr, value_type, gcc_p)
  1409.      value function;
  1410.      CORE_ADDR funcaddr;
  1411.      struct type *value_type;
  1412.      int gcc_p;
  1413.      /*ARGSUSED*/
  1414. {
  1415.   register enum type_code code = TYPE_CODE (value_type);
  1416.  
  1417.   if (code == TYPE_CODE_ERROR)
  1418.     error ("Function return type unknown.");
  1419.  
  1420.   if (code == TYPE_CODE_STRUCT ||
  1421.       code == TYPE_CODE_UNION ||
  1422.       code == TYPE_CODE_ARRAY)
  1423.     return USE_STRUCT_CONVENTION (gcc_p, value_type);
  1424.  
  1425.   return 0;
  1426. }
  1427.  
  1428. /* Store VAL so it will be returned if a function returns now.
  1429.    Does not verify that VAL's type matches what the current
  1430.    function wants to return.  */
  1431.  
  1432. void
  1433. set_return_value (val)
  1434.      value val;
  1435. {
  1436.   register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
  1437.   double dbuf;
  1438.   LONGEST lbuf;
  1439.  
  1440.   if (code == TYPE_CODE_ERROR)
  1441.     error ("Function return type unknown.");
  1442.  
  1443.   if (   code == TYPE_CODE_STRUCT
  1444.       || code == TYPE_CODE_UNION)    /* FIXME, implement struct return.  */
  1445.     error ("GDB does not support specifying a struct or union return value.");
  1446.  
  1447.   /* FIXME, this is bogus.  We don't know what the return conventions
  1448.      are, or how values should be promoted.... */
  1449.   if (code == TYPE_CODE_FLT)
  1450.     {
  1451.       dbuf = value_as_double (val);
  1452.  
  1453.       STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&dbuf);
  1454.     }
  1455.   else
  1456.     {
  1457.       lbuf = value_as_long (val);
  1458.       STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&lbuf);
  1459.     }
  1460. }
  1461.  
  1462. void
  1463. _initialize_values ()
  1464. {
  1465.   add_cmd ("convenience", no_class, show_convenience,
  1466.         "Debugger convenience (\"$foo\") variables.\n\
  1467. These variables are created when you assign them values;\n\
  1468. thus, \"print $foo=1\" gives \"$foo\" the value 1.  Values may be any type.\n\n\
  1469. A few convenience variables are given values automatically:\n\
  1470. \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
  1471. \"$__\" holds the contents of the last address examined with \"x\".",
  1472.        &showlist);
  1473.  
  1474.   add_cmd ("values", no_class, show_values,
  1475.        "Elements of value history around item number IDX (or last ten).",
  1476.        &showlist);
  1477. }
  1478.